package com.sz.app.oss.api.logistics.price;


import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.core.service.PriceComboService;
import com.sz.biz.logistics.core.dto.PriceComboDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Function: 价格套餐 controller <br>
 * Author: hpeng.wang <br>
 * Date: 2017-04-18 16:46:00.
 */

@RestController
@RequestMapping("${api}/biz/combos")
@Api(description = " ", tags = "035-005、价格-价格套餐")
public class PriceComboController extends BaseController {
    @Autowired
    private PriceComboService service;

    /**
     * 新增.
     */
    @ApiOperation(value = "新增", notes = "新增价格套餐<br>权限：" + Permissions.BIZ_MGT_BASE_PRICE_COMBO)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_PRICE_COMBO)
    public ResultDto save(@RequestBody @ApiParam(value = "要保存的数据", required = true) @Validated PriceComboDto dto) throws ParseException {
        int id = service.save(dto);
        ResultDto result = getSaveSuccessDto(MessagesCodes.MODULE_PRICE_COMBO);
        result.setData(id);
        return result;

    }

    /**
     * 批量删除
     *
     * @param uids 要删除的id集合
     * @return 操作结果
     */
    @ApiOperation(value = "删除", notes = "根据指定id集合删除数据<br>权限：" + Permissions.BIZ_MGT_BASE_PRICE_COMBO, httpMethod = "POST")
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_PRICE_COMBO)
    public ResultDto batchDelete(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<String> uids) {
        service.batchDelete(uids);
        ResultDto dto = getDeleteSuccessDto(MessagesCodes.MODULE_PRICE_COMBO);
        return dto;
    }

    /**
     * 修改.
     */
    @RequestMapping(value = "{uid}", method = {RequestMethod.POST})
    @ApiOperation(value = "修改", notes = "修改价格套餐<br>权限：" + Permissions.BIZ_MGT_BASE_PRICE_COMBO)
    @Transactional(propagation = Propagation.REQUIRED)
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_PRICE_COMBO)
    public ResultDto update(@PathVariable String uid, @RequestBody @Validated PriceComboDto dto) {
        dto.setUid(uid);
        service.update(dto);
        ResultDto result = getUpdateSuccessDto(MessagesCodes.MODULE_PRICE_COMBO);
        return result;
    }

    /**
     * 根据id查询数据
     *
     * @param uid 价格套餐的id
     * @return 查询到的单条价格套餐
     */
    @RequestMapping(value = "/{uid}", method = RequestMethod.GET)
    @ApiOperation(value = "查询_uid", notes = "根据id获取到的价格套餐")
    public ResultDto findByUid(@ApiParam(value = "价格套餐uid", required = true) @PathVariable String uid) {
        PriceComboDto dto = service.findDtoByUid(uid);
        ResultDto result = new ResultDto();
        result.setData(dto);
        return result;
    }

    /**
     * 根据条件获取数据集合
     *
     * @param q         参数信息
     * @param pageIndex 哪页
     * @param pageSize  分页大小
     * @param sort      排序字段的名称
     * @param asc       true asc false desc
     * @return 查询的数据集合
     */
    @ApiOperation(value = "查询", notes = "根据指定条件获取数据集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue
                    = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query",
                    defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue =
                    "true")
    })

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultDto find(@RequestParam(required = false) String q,
                          @RequestParam(required = false, defaultValue = "1") int pageIndex,
                          @RequestParam(required = false, defaultValue = "10") int pageSize,
                          @RequestParam(required = false) String sort,
                          @RequestParam(required = false, defaultValue = "true") Boolean asc
    ) {
        q = StringUtils.replaceSqlPattern(q);
        ResultDto result = service.findDtoByParam(new QueryParams(pageSize, pageIndex, q, sort, asc));
        return result;
    }

    @ApiOperation(value = "获取可用的套餐列表", notes = "获取可用的套餐列表")

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResultDto findCombos() {
        Map<String, String> result = service.findCombos();
        ResultDto resultDto = new ResultDto();
        resultDto.setData(result);
        return resultDto;
    }

    /**
     * 异步校验价格套餐的code
     *
     * @param code 价格套餐code
     * @param id   　价格套餐id，修改时传id，新增时为NULL
     * @return　校验的结果
     */
    @ApiOperation(value = "异步校验价格套餐名编码", notes = "异步校验价格套餐编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "价格套餐编码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "价格套餐id 修改时传id 新增时传空", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    public ResultDto checkCode(@RequestParam String code,
                               @RequestParam(required = false, defaultValue = "0") int id) {
        service.checkCode(id, code);
        return new ResultDto();
    }


}



